<?php

global $_GPC;
global $_W;
$cfg = $this->module['config'];
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if ($_GPC['_userid']) {
    header('location:' . $this->createMobileUrl('index'));
    exit(0);
}
if ($operation == 'display') {
    $user = $_GPC['user'];
} else {
    if ($operation == 'post') {
        $userid = $_GPC['user'];
        $pwd = $_GPC['pass'];
        $isremember = intval($_GPC['isremember']);
        if (!$userid || !$pwd) {
            exit(json_encode(array('success' => false, 'msg' => '用户名或者密码错误')));
        }
        $pwd = md5($_GPC['pass']);
        $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_user') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and useracount=:a and password=:b limit 1', array(':a' => $userid, ':b' => $pwd));
        if (!$item) {
            exit(json_encode(array('success' => false, 'msg' => '用户不存在或者密码错误')));
        }
        if (!$item['status']) {
            exit(json_encode(array('success' => false, 'msg' => '该用户还没有审核，请联系管理员')));
        }
        isetcookie('_userid', $item['id']);
        isetcookie('siteuniacid', $_W['uniacid']);
        isetcookie('siteroot', $_W['siteroot']);
        if ($isremember) {
            isetcookie('user', $_GPC['user'], 86400 * 7);
        }
        pdo_update('j_hmoney_user', array('lasttime' => TIMESTAMP), array('id' => $item['id']));
        load()->model('user');
        if ($item['docking']) {
            $record = user_single($item['docking']);
            $cookie = array();
            $cookie['uid'] = $record['uid'];
            $cookie['lastvisit'] = $record['lastvisit'];
            $cookie['lastip'] = $record['lastip'];
            $cookie['hash'] = md5($record['password'] . $record['salt']);
            $session = authcode(json_encode($cookie), 'encode');
            isetcookie('__session', $session);
            $status = array();
            $status['uid'] = $record['uid'];
            $status['lastvisit'] = TIMESTAMP;
            $status['lastip'] = CLIENT_IP;
            pdo_update('users', $status, array('uid' => $record['uid']));
            isetcookie('__uniacid', $_W['uniacid']);
            isetcookie('__uid', $record['uid']);
        }
        exit(json_encode(array('success' => true)));
    }
}
include $this->template($cfg['floder'] . '/login');